package com.hylanda.va.controller;

import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.OutputStream;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;
import com.hylanda.va.entity.TIncome;
import com.hylanda.va.model.ApiResponse;
import com.hylanda.va.model.IncomeQuery;
import com.hylanda.va.service.TIncomeService;

/** 
 * @author zhangy
 * @E-mail:blackoon88@gmail.com 
 * @qq:846579287
 * @version created at：2018年5月29日 下午6:10:30 
 * note
 */
@Controller
@RequestMapping("/income")
public class TIncomeController {

public static Logger logger = LoggerFactory.getLogger(TCostController.class);
	@Autowired
	private TIncomeService tIncomeService;
	private static final String prefix = "va/income/";
	@RequestMapping(value = { "/index" }, method = RequestMethod.GET)
	public String insert(HttpServletRequest request,
			HttpServletResponse response){
		return prefix+"index";
	}
	
	@RequestMapping(value = { "/upload" }, method = RequestMethod.GET)
	public String importexcel(HttpServletRequest request,
			HttpServletResponse response){
		return prefix+"upload";
	}
	
	@RequestMapping(value = {"/upload"}, method = RequestMethod.POST)
	public @ResponseBody ApiResponse  uploadReportFile(HttpServletRequest request,
			MultipartFile file,IncomeQuery incomeQuery) {
		//excel分析
		ApiResponse ar=tIncomeService.checkFile(file);
		if(ar!=null) return ar;
		//检查该月数据是否已上传 若上传删除以前数据
		tIncomeService.checkData(incomeQuery.getIncomeMonth());
		ar=tIncomeService.hanlderExcelData(file,incomeQuery.getIncomeMonth());
		if(ar!=null) return ar;
		return new ApiResponse("上传成功！","1");
	}
	//查询
	@RequestMapping(value = "/list")
	@ResponseBody
	public Page<TIncome> getList(IncomeQuery incomeQuery) {
		Page<TIncome> page=tIncomeService.findIncomeCriteria(incomeQuery.getPage(), incomeQuery.getSize(), incomeQuery);
		return page;
	}
	
	@RequestMapping(value="/getfilename", method = RequestMethod.GET)
	@ResponseBody
	public String getfilename(IncomeQuery incomeQuery){
		 String fileName=tIncomeService.genExcel(incomeQuery);
		 return fileName;
	}
	
	//下载报表
	@RequestMapping(value = "/download", method = RequestMethod.GET)
	public void downloadFile(@RequestParam("filename") String fileName,HttpServletResponse response) {
	    if (fileName != null) {
	        File file = new File("tmp" , fileName);
	        if (file.exists()) {
	        	response.setHeader("content-type", "application/octet-stream");
	            response.setContentType("application/octet-stream");// force-download设置强制下载不打开
	            response.addHeader("Content-Disposition", "attachment;fileName=" + fileName);// 设置文件名
	            byte[] buffer = new byte[1024];
	            FileInputStream fis = null;
	            BufferedInputStream bis = null;
	            try {
	                fis = new FileInputStream(file);
	                bis = new BufferedInputStream(fis);
	                OutputStream os = response.getOutputStream();
	                int i = bis.read(buffer);
	                while (i != -1) {
	                    os.write(buffer, 0, i);
	                    i = bis.read(buffer);
	                }
	            } catch (Exception e) {
	                e.printStackTrace();
	            } finally {
	                if (bis != null) {
	                    try {
	                        bis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	                if (fis != null) {
	                    try {
	                        fis.close();
	                    } catch (IOException e) {
	                        e.printStackTrace();
	                    }
	                }
	            }
	        }
	    }
	}
}
